<template>
  <div class="mod-user">
    <el-form :model="dataForm" :inline="true">
      <el-form-item>
        <el-input v-model="dataForm.shopName" v-bind:readonly="true" placeholder="选择商铺" clearable  @clear="clearShop"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="chooseShop()">选择商铺</el-button>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="dataForm.start"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择开始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="dataForm.end"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.orderNo" placeholder="支付订单号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button v-if="isAuth('center:notify:list')" @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <vxe-table
      :data="dataList"
      border
      highlight-hover-row
      show-footer
      v-loading="dataListLoading"
      :footer-method="footerMethod"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <vxe-table-column
        field="orderId"
        header-align="center"
        align="center"
        title="支付单号">
      </vxe-table-column>
      <vxe-table-column
        field="user.username"
        header-align="center"
        align="center"
        title="支付用户号">
      </vxe-table-column>
      <vxe-table-column
        field="shop.shopName"
        header-align="center"
        align="center"
        title="店铺名">
      </vxe-table-column>
      <vxe-table-column
        field="tradeNo"
        header-align="center"
        align="center"
        label="支付订单号">
      </vxe-table-column>
      <vxe-table-column
        field="result"
        header-align="center"
        align="center"
        title="通知响应结果">
      </vxe-table-column>
      <vxe-table-column
        field="status"
        header-align="center"
        align="center"
        title="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" size="small" type="danger">通知中</el-tag>
          <el-tag v-if="scope.row.status === 2" size="small" type="danger">通知成功</el-tag>
          <el-tag v-if="scope.row.status === 3" size="small" type="danger">通知失败</el-tag>
        </template>
      </vxe-table-column>
      <vxe-table-column
        field="order.amount"
        header-align="center"
        align="center"
        title="金额">
      </vxe-table-column>
      <vxe-table-column
        field="createTime"
        header-align="center"
        align="center"
        title="创建时间">
      </vxe-table-column>
      <vxe-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        title="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="showOrderInfoDialog(scope.row.orderId)">查看订单</el-button>
          <el-button type="text" size="small" @click="checkPayHandle(scope.row.orderId, scope.row.tradeNo, scope.row.order.channelId)">对账单</el-button>
        </template>
      </vxe-table-column>
    </vxe-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <el-dialog
      title="订单详情"
      :visible.sync="dialogVisible"
      width="70%">
      <order-info-view ref="orderInfoView"></order-info-view>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <alipay-trade-query v-if="alipayTradeQueryVisible" ref="alipayTradeQuery" @refreshDataList="getDataList"></alipay-trade-query>
    <shop-list v-if="shopListVisible" ref="shopList" @refreshDataList="findCompany"></shop-list>
  </div>
</template>

<script>
  import OrderInfoView from './orderInfoView'
  import AlipayTradeQuery from './alipay-trade-query'
  import ShopList from './shop-list'
  export default {
    data () {
      return {
        dialogVisible: false,
        alipayTradeQueryVisible: false,
        shopListVisible: false,
        dataForm: {
          shopName: '',
          cid: '',
          start: '',
          end: '',
          orderNo: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: []
      }
    },
    components: {
      AlipayTradeQuery,
      ShopList,
      OrderInfoView
    },
    activated () {
      this.getDataList()
    },
    methods: {
      showOrderInfoDialog (orderId) {
        this.dialogVisible = true
        this.$nextTick(() => {
          this.$refs.orderInfoView.getOrderInfo(orderId, true)
        })
      },
      clearShop () {
        this.dataForm.shopName = ''
        this.dataForm.cid = ''
      },
      footerMethod ({ columns, data }) {
        const sums = []
        columns.forEach((column, columnIndex) => {
          if (columnIndex === 0) {
            sums.push('总金额')
          } else {
            let sumCell = null
            this.$http({
              url: this.$http.adornUrl('/pig-center-biz/center/notify/total'),
              method: 'get',
              params: this.$http.adornParams({
                'cid': this.dataForm.cid,
                'start': this.dataForm.start,
                'end': this.dataForm.end,
                'orderNo': this.dataForm.orderNo
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                switch (column.property) {
                  case 'order.amount':
                    sumCell = data.total
                    break
                }
                sums.push(sumCell)
              }
            })
          }
        })
        // 返回一个二维数组的表尾合计
        return [ sums ]
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/pig-center-biz/center/notify/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'cid': this.dataForm.cid,
            'start': this.dataForm.start,
            'end': this.dataForm.end,
            'orderNo': this.dataForm.orderNo
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
            this.$message.error(data.msg)
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      checkOrderHandle (val) {
        console.log(val)
      },
      findCompany (id, name) {
        this.dataForm.cid = id
        this.dataForm.shopName = name
      },
      chooseShop () {
        this.shopListVisible = true
        this.$nextTick(() => {
          this.$refs.shopList.getDataList()
        })
      },
      checkPayHandle (id, tradeNo, channelId) {
        if (channelId === 'ALIPAY_PC') {
          this.$http({
            url: this.$http.adornUrl(`/pig-pay/aliPay/tradeQuery`),
            method: 'post',
            params: this.$http.adornParams({
              'outTradeNo': id,
              'tradeNo': tradeNo
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.alipayTradeQueryVisible = true
              this.$nextTick(() => {
                this.$refs.alipayTradeQuery.init(data.alipayInfo)
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      }
    }
  }
</script>
